package org.yray.sip.config.sip;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.yray.sip.rtp.server.UrtcpServer;
import org.yray.sip.rtp.server.UrtcpServerHandler;
import org.yray.sip.rtp.server.UrtpServer;
import org.yray.sip.rtp.server.UrtpServerHandler;
import org.yray.sip.server.SipServer;
import org.yray.sip.server.SipServerContext;
import org.yray.sip.server.SipServerHandler;
import org.yray.sip.utils.IpUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableConfigurationProperties({SipProperties.class})
public class SipAutoAutoConfiguration {

    private final SipProperties properties;

    public SipAutoAutoConfiguration(SipProperties sipProperties)
    {
        this.properties=sipProperties;
    }

    @Bean
    public ExecutorService createSipExecutor()
    {
        ExecutorService executorService= new ThreadPoolExecutor(6, 6, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

        //internal Sip报文服务器
        SipServer internalSipServer=internalSipServer();
        //internal rtp报文服务器
        UrtpServer internalUrtpServer=internalUrtpServer();
        //internal rtcp报文服务器
        UrtcpServer internalUrtcpServer=internalUrtcpServer();

        SipServer externalSipServer=externalSipServer();

        UrtpServer externalUrtpServer=externalUrtpServer();

        UrtcpServer externalUrtcpServer=externalUrtcpServer();

        executorService.execute(internalSipServer);
        executorService.execute(internalUrtpServer);
        executorService.execute(internalUrtcpServer);
        executorService.execute(externalSipServer);
        executorService.execute(externalUrtpServer);
        executorService.execute(externalUrtcpServer);

        return executorService;
    }

    @Bean
    public UrtcpServer internalUrtcpServer()
    {
        return new UrtcpServer(new UrtcpServerHandler(internalSsc()));
    }


    @Bean
    public UrtcpServer externalUrtcpServer()
    {
        return new UrtcpServer(new UrtcpServerHandler(externalSsc()));
    }


    @Bean
    public UrtpServer internalUrtpServer()
    {
        return new UrtpServer(new UrtpServerHandler(internalSsc()));
    }

    @Bean
    public UrtpServer externalUrtpServer()
    {
        return new UrtpServer(new UrtpServerHandler(externalSsc()));
    }

    @Bean
    public SipServer internalSipServer()
    {
        return new SipServer(new SipServerHandler(internalSsc()));
    }

    @Bean
    public SipServer externalSipServer()
    {
        return new SipServer(new SipServerHandler(externalSsc()));
    }


    @Bean
    public SipServerContext internalSsc()
    {
        SipServerContext internalSsc =new SipServerContext(properties.getInternalSipIp(),
                properties.getInternalSipPort(),
                properties.getRunIp()==null? IpUtil.getLocalIp():properties.getRunIp(),
                properties.getRunPort(),
                properties.getRecordPath());
        return internalSsc;
    }

    @Bean
    public SipServerContext externalSsc()
    {
        SipServerContext internalSsc =new SipServerContext(properties.getExternalSipIp(),
                properties.getExternalSipPort(),
                properties.getRunIp()==null? IpUtil.getLocalIp():properties.getRunIp(),
                properties.getRunPort()+2,
                properties.getRecordPath());
        return internalSsc;
    }
}
